﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;
using Unity.Interception.PolicyInjection.Pipeline;
using Unity.Interception.PolicyInjection.Policies;

namespace PrismMetroSample.Shell.Aop
{
    public class DmoLogCallHandler : ICallHandler
    {
        public int Order
        {
            get; set;
        }

        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            //可以做参数判断
            //可以抛出异常
            /*User user = input.Inputs[0] as User;
            if (user.Password.Length < 10)
            {
                return input.CreateExceptionMethodReturn(new Exception("密码长度不能小于10位"));
            }
            Console.WriteLine("参数检测无误");*/

            Debugger.Log(0, "AOP", $"LogCallHandler Invoke befroe: {input.MethodBase.Name}\r\n");

            Debugger.Log(0, "AOP", string.Format("Arguments.Count:{0}\r\n", input.Arguments.Count));
            var result = getNext().Invoke(input, getNext);

            if (result.Exception != null)
            {
                Console.WriteLine("");
            }

            Debugger.Log(0, "AOP", "LogCallHandler Invoke end\r\n");
            Debugger.Log(0, "AOP", string.Format("返回结果：{0}\r\n", result.ReturnValue));
            return result;
        }
    }

    public class DmoLogCallHandlerAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new DmoLogCallHandler();
        }
    }
}
